﻿//常用Js方法
//判断字符串中是否有指定值
String.prototype.contains = function(str) { return (this.indexOf(str) > -1); };
//去除字符串首尾指定字符,如果空时则去除前后空隔
String.prototype.trim = function(s) { if (s) return this.trimEnd(s).trimStart(s); else return this.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, ''); };
//去除字符串结尾指定字符
String.prototype.trimEnd = function(s) { if (this.endsWith(s)) { return this.substring(0, this.length - s.length); } return this; };
//去除字符串开始指定字符
String.prototype.trimStart = function(s) { if (this.startsWith(s)) { return this.slice(s.length); } return this; };
//判断指定字符在字符串最前
String.prototype.startsWith = function(str) { return (this.indexOf(str) == 0); };
//判断指定字符在字符串最后
String.prototype.endsWith = function(str) { return (str.length <= this.length && this.substr(this.length - str.length, str.length) == str); };
//移除字符串中指定位置长度的字符
String.prototype.remove = function(start, l) { var str1 = this.substring(0, start); var str2 = this.substring(start + l, this.length); return str1 + str2; }
//将字符插入指定位置
String.prototype.insert = function(index, str) { var str1 = this.substring(0, index); var str2 = this.substring(index, this.length); return str1 + str + str2; }
//判断字符串是否为null或空
String.isNullOrEmpty = function(str) { return str; };
//格式化字符串
String.format = function() { var str = arguments[0]; for (var i = 1; i < arguments.length; i++) { var reg = new RegExp("\\{" + (i - 1) + "\\}", "ig"); str = str.replace(reg, arguments[i]); } return str; };
//指定对象是否存在于当前数组中
Array.prototype.contains = function(val) { for (var i = 0; i < this.length; i++) { if (val == this[i]) return true; } return false; }

//通过ID查找Element对象
function $(id) { return (typeof id == 'string' ? document.getElementById(id) : id); }
function $$(name) {return document.getElementsByName(name);}
//设置/获取节点的属性 value 未传的时候是获取属性
//element : 节点
//attrName: 属性名称
//[value] : 值（如果为空就返回属性值）
function attr(element, attrName, value) { if (value) element.setAttribute(attrName, value); else return element.getAttribute(attrName); }
//对象事件注册
//elm   :节点
//n     :事件名称比如click,dbclick,change,可以在前面带on也可以不带
//h     :事件处理函数
function addHandler(elm, n, h) { if (n.indexOf('on', 0) == 0) { n = n.substring(2, n.length); } if (!browser.isIE) { elm.addEventListener(n, h, false); } else { elm.attachEvent("on" + n, h); } }
//移除事件注册
//e :节点
//n :事件名称比如click,change
//h :事件处理函数
function removeHandler(e, n, h) { if (n.indexOf('on', 0) >= 0) { n = n.substring(2, n.length); } if (!browser.isIE) { e.removeEventListener(n, h, false); } else { e.detachEvent("on" + n, h); } }
//设置对象的样式
//例如 : setStyle($('id'),{ display:'none',position : 'absolute',zIndex : 999 });
function setStyle(e, s) { for (var k in s) { e.style[k] = s[k]; } }
//增加CSS Class
function addCssClass(e, c) { if (!e.className) { e.className = c; return; } if (e.className.indexOf(c) > -1) return; e.className += " " + c; }
//删除CSS Class
function removeCssClass(e, c) {
    var cn = e.className;
    if (!cn) return;
    var indx = cn.indexOf(c);
    if (indx == -1) return; if (cn.length == c.length) { cn = ""; }
    else if (indx == 0) cn = cn.remove(0, c.length).trim();
    else if (indx > 0) cn = cn.remove(indx, c.length).trim().replace("  ", " ");
    e.className = cn;
}

//写COOKIE
function writeCookie(name, value, hours) {
    var expire = "";
    if (hours) {
        expire = new Date((new Date()).getTime() + hours * 3600000);
        expire = "; expires=" + expire.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expire;
}

//读取cookie
function readCookie(name) {
    var cookieValue = "";
    var search = name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    return cookieValue;
}


//判断浏览器类型
var browser = {
    isIE: navigator.userAgent.toLowerCase().contains('msie'),
    isIE5: navigator.userAgent.toLowerCase().contains('msie 5'),
    isIE6: navigator.userAgent.toLowerCase().contains('msie 6'),
    isIE7: navigator.userAgent.toLowerCase().contains('msie 7'),
    isIE8: navigator.userAgent.toLowerCase().contains('msie 8'),
    isGecko: navigator.userAgent.toLowerCase().contains('gecko'),
    isSafari: navigator.userAgent.toLowerCase().contains('safari'),
    isOpera: navigator.userAgent.toLowerCase().contains('opera')
};